package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.dto.SysHomepageResDto;
import com.ruoyi.common.core.domain.entity.system.SysResource;
import com.ruoyi.common.core.domain.entity.system.SysScenic;
import com.ruoyi.common.core.domain.vo.MiniScenicVo;
import com.ruoyi.common.oss.OssManager;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.SysResourceMapper;
import com.ruoyi.system.service.ISysResourceService;
import com.ruoyi.system.service.ISysScenicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysResourceServiceImpl implements ISysResourceService {
    @Resource
    private SysResourceMapper sysResourceMapper;

    @Autowired
    private ISysScenicService sysScenicService;

    @Autowired
    private OssManager ossManager;

    /**
     * 当前是否是工作日。
     */
    private boolean isWorkDay = false;

    @Override
    public int updateSlidePictures(String[] imageNameArr) {
        if (imageNameArr.length > 5) return 0;

        String[] newArr = new String[5];
        for (int i = 0; i < imageNameArr.length; i++) {
            newArr[i] = imageNameArr[i];
        }

        if (imageNameArr.length < 5) {
            for (int i = imageNameArr.length; i < 5; i++) {
                newArr[i] = " ";
            }
        }

        return sysResourceMapper.updateSlidePictures(newArr[0], newArr[1], newArr[2], newArr[3], newArr[4]);
    }

    @Override
    public SysResource getSysResourceByName() {
        return sysResourceMapper.getSysResourceByName();
    }

    @Override
    public List<MiniScenicVo> getScenicInfoList() {
        int dayOfWeek = DateUtil.dayOfWeek(new Date());

        if (dayOfWeek == 1 || dayOfWeek == 7) { //1为周日，7为周六
            isWorkDay = false;
        } else {
            isWorkDay = true;
        }

        List<SysScenic> sysScenics = sysScenicService.selectSysScenicList(new SysScenic());
        List<MiniScenicVo> miniScenicVos = sysScenics.stream()
                //先过滤掉全国景区(scenicId=100)无疆技术(scenicId=101)
                .filter(sysScenic -> sysScenic.getScenicId() != 100 && sysScenic.getScenicId() != 101)
                //再过滤掉所有未被停用的景区。景区状态：（0正常,1停用）
                .filter(sysScenic -> !sysScenic.getStatus().equals("1"))
                //再过滤掉所有当前未处于运营状态的景区。景区运营状态： (0：运营中 1：已停止运营)
                .filter(sysScenic -> !sysScenic.getOperationStatus().equals("1"))
                .map(sysScenic -> {
                    MiniScenicVo miniScenicVo = new MiniScenicVo();
                    BeanUtils.copyProperties(sysScenic, miniScenicVo);
                    String startTimeOfWorkday = sysScenic.getStartTimeOfWorkday();
                    String endTimeOfWorkday = sysScenic.getEndTimeOfWorkday();
                    String startTimeOfWeekday = sysScenic.getStartTimeOfWeekday();
                    String endTimeOfWeekday = sysScenic.getEndTimeOfWeekday();

                    if (isWorkDay) {
                        if (StringUtils.isNotEmpty(startTimeOfWorkday) && StringUtils.isNotEmpty(endTimeOfWorkday)) {
                            miniScenicVo.setStartTimeOfToday(startTimeOfWorkday.substring(0, startTimeOfWorkday.lastIndexOf(":")));
                            miniScenicVo.setEndTimeOfToday(endTimeOfWorkday.substring(0, endTimeOfWorkday.lastIndexOf(":")));
                        } else {
                            miniScenicVo.setStartTimeOfToday("9:00");
                            miniScenicVo.setEndTimeOfToday("18:00");
                        }

                    } else {
                        if (StringUtils.isNotEmpty(startTimeOfWeekday) && StringUtils.isNotEmpty(endTimeOfWeekday)) {
                            miniScenicVo.setStartTimeOfToday(startTimeOfWeekday.substring(0, startTimeOfWeekday.lastIndexOf(":")));
                            miniScenicVo.setEndTimeOfToday(endTimeOfWeekday.substring(0, endTimeOfWeekday.lastIndexOf(":")));
                        } else {
                            miniScenicVo.setStartTimeOfToday("9:00");
                            miniScenicVo.setEndTimeOfToday("18:00");
                        }

                    }

                    return miniScenicVo;
                }).collect(Collectors.toList());

        //将查询到的所有景区按照创建时间倒序排列，即后创建的景区排在先创建的景区之前
        if (miniScenicVos.size() > 0) {
            Collections.sort(miniScenicVos, (o1, o2) -> {
                if (o1.getCreateTime().getTime() >= o2.getCreateTime().getTime()) {
                    return -1;
                } else {
                    return 1;
                }
            });
        }

        return miniScenicVos;
    }

    @Override
    public int updateBoatingNoticeFile(String boatingNoticeFilename) {
        return sysResourceMapper.updateBoatingNoticeFile(boatingNoticeFilename);
    }

    @Override
    public int deleteBoatingNoticeFile() {
        return sysResourceMapper.deleteBoatingNoticeFile();
    }

    @Override
    public int updateBoatingGuideFile(String boatingGuideFilename) {
        return sysResourceMapper.updateBoatingGuideFile(boatingGuideFilename);
    }

    @Override
    public int deleteBoatingGuideFile() {
        return sysResourceMapper.deleteBoatingGuideFile();
    }

    @Override
    public int updateBaochuanIcon(String baochuanIconName) {
        return sysResourceMapper.updateBaochuanIcon(baochuanIconName);
    }

    @Override
    public int updatePinchuanIcon(String pinchuanIconName) {
        return sysResourceMapper.updatePinchuanIcon(pinchuanIconName);
    }

    @Override
    public int deleteBaochuanIcon() {
        return sysResourceMapper.deleteBaochuanIcon();
    }

    @Override
    public int deletePinchuanIcon() {
        return sysResourceMapper.deletePinchuanIcon();
    }

    @Override
    public int updatePinchuanDescText(String pinchuanDescText) {
        return sysResourceMapper.updatePinchuanDescText(pinchuanDescText);
    }

    @Override
    public int updateBaochuanDescText(String baochuanDescText) {
        return sysResourceMapper.updateBaochuanDescText(baochuanDescText);
    }

    @Override
    public int updateBoatingNoticeContent(String boatingNoticeContent) {
        return sysResourceMapper.updateBoatingNoticeContent(boatingNoticeContent);
    }

    @Override
    public int updateBoatingGuideContent(String boatingGuideContent) {
        return sysResourceMapper.updateBoatingGuideContent(boatingGuideContent);
    }

    @Override
    public SysHomepageResDto getHomepageRes() {
        SysResource sysResource = sysResourceMapper.getSysResourceByName();
        SysHomepageResDto homepageResDto = new SysHomepageResDto();
        //设置首页轮播图图片集合
        List<String> slidePics = new ArrayList<>();
        checkAndAddSlideName(slidePics, sysResource.getSlidePic1Name());
        checkAndAddSlideName(slidePics, sysResource.getSlidePic2Name());
        checkAndAddSlideName(slidePics, sysResource.getSlidePic3Name());
        checkAndAddSlideName(slidePics, sysResource.getSlidePic4Name());
        checkAndAddSlideName(slidePics, sysResource.getSlidePic5Name());
        homepageResDto.setSlidePics(slidePics);
        //设置乘船须知文本内容
        homepageResDto.setBoatingNoticeContent(sysResource.getBoatingNoticeContent());
        //设置乘船指南文本内容
        homepageResDto.setBoatingGuideContent(sysResource.getBoatingGuideContent());
        //设置由隐私条款文件等文件组成的集合
        List<String> privacyFiles = new ArrayList<>();
        String policyFilename = sysResource.getPrivacyPolicyFilename();
        if (StringUtils.isNotEmpty(policyFilename)) {
            String[] fileNames = policyFilename.split(",");
            for (String fileName : fileNames) {
                URL url = ossManager.generatePresignedUrl(fileName);
                if (Objects.nonNull(url)) {
                    privacyFiles.add(url.toString());
                }
            }
        }
        homepageResDto.setPrivacyFiles(privacyFiles);

        //获取首页包船与拼船的描述文字及小图标
        String baochuanIcon = sysResource.getBaochuanIcon();
        String pinchuanIcon = sysResource.getPinchuanIcon();
        URL url1 = ossManager.generatePresignedUrl(baochuanIcon);
        URL url2 = ossManager.generatePresignedUrl(pinchuanIcon);
        if (Objects.nonNull(url1)) {
            homepageResDto.setBaochuanIcon(url1.toString());
        }
        if (Objects.nonNull(url2)) {
            homepageResDto.setPinchuanIcon(url2.toString());
        }
        homepageResDto.setBaochuanDescText(sysResource.getBaochuanDescText());
        homepageResDto.setPinchuanDescText(sysResource.getPinchuanDescText());

        homepageResDto.setBoatingGuideVideoUrl(sysResource.getBoatingGuideVideoUrl());
        homepageResDto.setAboutWe(sysResource.getAboutWe());

        return homepageResDto;
    }

    @Override
    public int updatePrivacyFiles(String privacyFileName) {
        return sysResourceMapper.updatePrivacyFiles(privacyFileName);
    }

    @Override
    public int updateAboutWe(String aboutWeContent) {
        return sysResourceMapper.updateAboutWe(aboutWeContent);
    }

    private void checkAndAddSlideName(List<String> slidePics, String picName) {
        if (!StringUtils.isEmpty(picName)) {
            URL url = ossManager.generatePresignedUrl(picName);
            if (Objects.nonNull(url)) {
                slidePics.add(url.toString());
            }
        }
    }

}
